import time

from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service



# 账号密码数据
account = '20207598'
password_1 = 'Hxh20011214'
password_2 = 'a146922'

# 新派实训
url_3 = 'https://zswxy.xinpaikeji.com/user/login'

url_2 = 'https://swxykdsx.pinlankeji.com/user/login'

def type_account(password):
    driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(account)
    driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
    driver.find_element(By.XPATH, '//*[@id="remember"]').click()


def adjust_verify():
    try:
        driver.implicitly_wait(2)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div[1]/div[4]/div/div/div[2]/div[1]/a').click()
    except exceptions.NoSuchElementException:
        print(str(exceptions))
        driver.find_element(By.PARTIAL_LINK_TEXT, '确定').click()
        print('验证码错误！请看清楚了好好输入！')
        return input_verify()


def input_verify():
    verify = input('请输入登录验证码：（作者暂时没有水平破解高难度随机验证码）')
    driver.find_element(By.XPATH, '//*[@id="code"]').send_keys(verify)
    driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[5]/div/input[2]').click()
    adjust_verify()


def get_video_message(url):
    latest_video = driver.current_url
    replace_url = (f'{url}')[:-11]
    video_message = latest_video.replace(replace_url, '')
    display_time = driver.find_element(By.XPATH,
                                       '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[2]/div[8]').text
    lst_videoTime = list(display_time.replace('00:00 / ', ''))
    video_minute = int(lst_videoTime[0] + lst_videoTime[1])
    video_second = int(lst_videoTime[3] + lst_videoTime[4])
    print('\n当前页面网课为：' + driver.find_element(By.XPATH, f'//a[@href="{video_message}"]').text)
    print('视频总时长为：' + str(video_minute) + '分' + str(video_second) + '秒\n\n')


def open_page3():
    driver.get(url_3)
    type_account(password_2)
    input_verify()
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[1]/div[2]/div[6]/div[1]/a').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[9]/canvas').click()
    get_video_message(url_3)


def open_page2():
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get(url_2)
    type_account(password_1)
    input_verify()
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[1]/div[2]/div[6]/div[1]/a').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[9]/canvas').click()
    get_video_message(url_2)


edge_Options = webdriver.EdgeOptions()
edge_Options.add_argument("--mute-audio")
s = Service()
driver = webdriver.Edge(service=s, options=edge_Options)
# 设置窗体大小
driver.set_window_size(height=600, width=400)
open_page3()

open_page2()
